//
//  RotateWithAction.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-7-16.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
//
//  自定义动作：由移动改变角度

#import "RotateWithAction.h"

@implementation RotateWithAction

@synthesize innerAction = innerAction_;
@synthesize innerTarget = innerTarget_;

+(id) actionWithAction:(CCActionInterval*)action
{
    return [[[self alloc] initWithAction:action] autorelease];
}

-(id) initWithAction:(CCActionInterval*)action;
{
    if( (self=[super init]) ) 
    {
		self.innerAction = action;
	}
	return self;
}

-(id) copyWithZone: (NSZone*) zone
{
    CCAction *copy = [[[self class] allocWithZone:zone] initWithAction:[[innerAction_ copy] autorelease]];
    return copy;
}

-(void) dealloc
{
    [innerAction_ release];
	[super dealloc];
}

-(void) startWithTarget:(id)aTarget
{
    self.innerTarget = aTarget;
    [super startWithTarget:aTarget];
	[innerAction_ startWithTarget:aTarget];
}


-(void) stop
{ 
    [innerAction_ stop];
	[super stop];
}

-(void) step:(ccTime) dt
{
    CGPoint prePos = innerTarget_.position;
    [innerAction_ step: dt];
    CGPoint curPos = innerTarget_.position;
    float tan = -(curPos.y - prePos.y) / (curPos.x - prePos.x);
    float degree = atan(tan);
    degree = CC_RADIANS_TO_DEGREES(degree) + 90;
    innerTarget_.rotation = degree;
}

-(void) setInnerAction:(CCActionInterval*) pAction
{
    if(innerAction_ != pAction)
    {
        [innerAction_ release];
        innerAction_ = pAction;
        [innerAction_ retain];
    }
}

-(BOOL) isDone
{
    return [innerAction_ isDone];
}

- (CCActionInterval *) reverse
{
    return [[self class] actionWithAction:[innerAction_ reverse]];

}

@end
